/**
 * 
 */
package com.nh.action;

import java.sql.Timestamp;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.nh.entity.NgNewsCategory;
import com.nh.entity.NgNewsInfo;
import com.nh.entity.NgNewsInfoContentHtml;
import com.nh.entity.NgNewsInfoContentPicture;
import com.nh.entity.NgNewsInfoContentVideo;

/**
 * 项目名称：NotoginsengManage
 * 
 * 类名称：NewsAction
 * 
 * 类描述： 信息中心
 * 
 * 创建人：李兴建
 * 
 * E-mail:lxj08193@163.com
 * 
 * 创建时间：2012-11-1 下午10:40:48
 * 
 * 修改人：李兴建
 * 
 * 修改时间：2012-11-1 下午10:40:48
 * 
 * 修改备注：
 * 
 * @version
 */
public class NewsAction extends BaseAction {
	private NgNewsInfo newsInfo;
	private List<NgNewsCategory> categoryList;
	private String optionStringId;
	private String htmlContent;
	private NgNewsInfoContentHtml newsInfoContentHtml;
	private NgNewsInfoContentVideo newsInfoContentVideo;
	private Long optionId;

	private String headImage;

	private String hindGroupImagInfo;
	
	//0:图片位 1:资讯 2：图组  3：视频
	private int optionType;

	private String content;
	private List<NgNewsInfoContentPicture> infoContentPictures;
	/**
	 * 信息列表
	 * 
	 * @return
	 */
	public String newListPage() {
		Criteria criteria = this.createCriteria(NgNewsInfo.class);
		criteria.add(Restrictions.between("state", 0, 1));
		criteria.addOrder(Order.desc("createTime"));
		criteria.addOrder(Order.desc("oderView"));
		this.queryByPage(criteria, "newsAction!newListPage.action", 10);
		return "newListPage";
	}

	/**
	 * 删除信息列表
	 * 
	 * @return
	 */
	public String newsRemoveListPage() {
		Criteria criteria = this.createCriteria(NgNewsInfo.class);
		criteria.add(Restrictions.eq("state", 2));
		criteria.addOrder(Order.desc("createTime"));
		criteria.addOrder(Order.desc("oderView"));
		this.queryByPage(criteria, "newsAction!newsRemoveListPage.action", 10);
		return "newsRemoveListPage";
	}

	/**
	 * HTML信息查询
	 * 
	 * @return
	 */
	public String newInfoQuery0Page() {
		Criteria criteria = this.createCriteria(NgNewsInfo.class);
		criteria.add(Restrictions.eq("head", 0));
		criteria.add(Restrictions.eq("infotype", 0));
		criteria.addOrder(Order.desc("createTime"));
		criteria.addOrder(Order.desc("oderView"));
		this.queryByPage(criteria, "newsAction!newInfoQuery0Page.action", 10);
		this.setTitle("HTML信息查询");
		return "newsHTMLList";
	}

	/**
	 * 图片位信息查询
	 * 
	 * @return
	 */
	public String newInfoQuery1Page() {
		Criteria criteria = this.createCriteria(NgNewsInfo.class);
		// criteria.add(Restrictions.eq("state", 0));
		criteria.add(Restrictions.eq("head", 1));
		criteria.addOrder(Order.desc("createTime"));
		criteria.addOrder(Order.desc("oderView"));
		this.queryByPage(criteria, "newsAction!newInfoQuery1Page.action", 10);
		this.setTitle("图片位信息查询");
		return "picturePositionList";
	}

	/**
	 * 图组信息查询
	 * 
	 * @return
	 */
	public String newInfoQuery2Page() {
		Criteria criteria = this.createCriteria(NgNewsInfo.class);
		criteria.add(Restrictions.eq("infotype", 1));
		criteria.addOrder(Order.desc("createTime"));
		criteria.addOrder(Order.desc("oderView"));
		this.queryByPage(criteria, "newsAction!newInfoQuery2Page.action", 10);
		this.setTitle("图组信息查询");
		return "pictureNesList";
	}

	/**
	 * 视频信息查询
	 * 
	 * @return
	 */
	public String newInfoQuery3Page() {
		Criteria criteria = this.createCriteria(NgNewsInfo.class);
		criteria.add(Restrictions.eq("infotype", 2));
		criteria.addOrder(Order.desc("createTime"));
		criteria.addOrder(Order.desc("oderView"));
		this.queryByPage(criteria, "newsAction!newInfoQuery3Page.action", 10);
		this.setTitle("视频信息查询");
		return "videoNesList";
	}

	/**
	 * 等待发布
	 * 
	 * @return
	 */
	public String newsPulishPage() {
		Criteria criteria = this.createCriteria(NgNewsInfo.class);
		criteria.add(Restrictions.eq("state", 0));
		criteria.addOrder(Order.desc("createTime"));
		criteria.addOrder(Order.desc("oderView"));
		this.queryByPage(criteria, "newsAction!newsPulishPage.action", 10);
		this.setTitle("等待发布信息");
		return "newsPulishPage";
	}

	/**
	 * 已经发布
	 * 
	 * @return
	 */
	public String newsPulishedPage() {
		Criteria criteria = this.createCriteria(NgNewsInfo.class);
		criteria.add(Restrictions.eq("state", 1));
		criteria.addOrder(Order.desc("createTime"));
		criteria.addOrder(Order.desc("oderView"));
		this.queryByPage(criteria, "newsAction!newsPulishedPage.action", 10);
		return "newsPulishedPage";
	}

	/**
	 * 图片位创建页面
	 * 
	 * @return
	 */
	public String picturePositionAdd() {
		newsInfo = new NgNewsInfo();
		newsInfo.setInfotype(0);
		newsInfo.setState(0);
		newsInfo.setHead(1);
		newsInfo.setOderView(0);
		newsInfo.setHeadImg("attached/syste_media/sys_ico_2.jpg");
		headImage = newsInfo.getHeadImg();
		newsInfoContentHtml = new NgNewsInfoContentHtml();
		content = "";
		this.setTitle("图片位信息创建");
		return "picturePositionAdd";
	}

	/**
	 * HTML网页信息创建
	 * 
	 * @return
	 */
	public String newsAdd() {
		newsInfo = new NgNewsInfo();
		newsInfo.setInfotype(0);
		newsInfo.setState(0);
		newsInfo.setHead(0);
		newsInfo.setOderView(0);
		newsInfo.setHeadImg("attached/syste_media/sys_ico_2.jpg");
		headImage = newsInfo.getHeadImg();
		newsInfoContentHtml = new NgNewsInfoContentHtml();
		content = "";
		this.setTitle("HTML网页信息创建");
		return "newsAdd";
	}

	/**
	 * 图片信息页面
	 * 
	 * @return
	 */
	public String newsAddPicture() {
		newsInfo = new NgNewsInfo();
		newsInfo.setInfotype(0);
		newsInfo.setState(0);
		newsInfo.setHead(0);
		newsInfo.setOderView(0);
		newsInfo.setHeadImg("attached/syste_media/sys_ico_2.jpg");
		headImage = newsInfo.getHeadImg();
		this.setTitle("图片组信息创建");
		return "newsAddPicture";
	}

	/**
	 * 视频信息页面
	 * 
	 * @return
	 */
	public String newsAddVideo() {
		newsInfo = new NgNewsInfo();
		newsInfo.setInfotype(0);
		newsInfo.setState(0);
		newsInfo.setHead(0);
		newsInfo.setOderView(0);
		newsInfo.setHeadImg("attached/syste_media/sys_ico_2.jpg");
		headImage = newsInfo.getHeadImg();
		newsInfoContentVideo = new NgNewsInfoContentVideo();
		this.setTitle("视频信息创建");
		return "newsAddVideo";
	}

	/**
	 * 视频信息添加保存
	 * 
	 * @return
	 */
	public String videoNewsSave() {
		newsInfo.setNewsCategory(null);
		newsInfo.setCreateAdmin("test");
		if (newsInfo.getState() == 1) {
			newsInfo.setPublishTime(new Timestamp(System.currentTimeMillis()));
		}
		newsInfo.setCreateTime(new Timestamp(System.currentTimeMillis()));
		newsInfo.setInfotype(2);
		newsInfo.setHeadImg(headImage);
		if (null != newsInfo && null != newsInfo.getId()) {
			this.getBaseService().saveOrUpdate(newsInfo, NgNewsInfo.class);
		} else {
			this.getBaseService().save(newsInfo);
		}
		newsInfoContentVideo.setCreateTime(new Timestamp(System.currentTimeMillis()));
		newsInfoContentVideo.setNewsInfoId(newsInfo.getId());
		newsInfoContentVideo.setDescription(content);

		if (newsInfoContentVideo != null && null != newsInfoContentVideo.getId()) {
			this.getBaseService().saveOrUpdate(newsInfoContentVideo, NgNewsInfoContentVideo.class);
		} else {
			this.getBaseService().save(newsInfoContentVideo);
		}
		return newInfoQuery3Page();

	}

	/**
	 * 保存信息位信息
	 * 
	 * @return
	 */
	public String picturePositionNewsSave() {
		newsInfo.setCreateAdmin(this.getCurrentAdmin().getRealEname());
		if (newsInfo.getState() == 1) {
			newsInfo.setPublishTime(new Timestamp(System.currentTimeMillis()));
		}
		newsInfo.setCreateTime(new Timestamp(System.currentTimeMillis()));
		newsInfo.setHeadImg(headImage);
		newsInfo.setInfotype(0);
		if (null != newsInfo && null != newsInfo.getId()) {
			this.getBaseService().saveOrUpdate(newsInfo, NgNewsInfo.class);
		} else {
			this.getBaseService().save(newsInfo);
		}
		newsInfoContentHtml.setCreateTime(new Timestamp(System.currentTimeMillis()));
		newsInfoContentHtml.setHtmlContent(htmlContent);
		newsInfoContentHtml.setNewsInfoId(newsInfo.getId());
		if (null != newsInfoContentHtml.getId()) {
			this.getBaseService().saveOrUpdate(newsInfoContentHtml, NgNewsInfoContentHtml.class);
		} else {
			this.getBaseService().save(newsInfoContentHtml);
		}

		return newInfoQuery1Page();
	}

	/**
	 * 保存信息
	 * 
	 * @return
	 */
	public String hemlNewsSave() {

		NgNewsCategory newsCategory = this.getBaseService().findById(Long.parseLong(optionStringId), NgNewsCategory.class);
		newsInfo.setNewsCategory(newsCategory);

		newsInfo.setCreateAdmin("test");
		if (newsInfo.getState() == 1) {
			newsInfo.setPublishTime(new Timestamp(System.currentTimeMillis()));
		}
		newsInfo.setCreateTime(new Timestamp(System.currentTimeMillis()));
		newsInfo.setHeadImg(headImage);
		newsInfo.setInfotype(0);
		if (null != newsInfoContentHtml && null != newsInfoContentHtml.getId()) {
			this.getBaseService().saveOrUpdate(newsInfo, NgNewsInfo.class);
		} else {
			this.getBaseService().save(newsInfo);
		}
		if (newsInfo.getInfotype() == 0) {
			if (null == newsInfoContentHtml) {
				newsInfoContentHtml = new NgNewsInfoContentHtml();
			}
			newsInfoContentHtml.setCreateTime(new Timestamp(System.currentTimeMillis()));
			newsInfoContentHtml.setHtmlContent(htmlContent);
			newsInfoContentHtml.setNewsInfoId(newsInfo.getId());
			if (null != newsInfoContentHtml.getId()) {
				this.getBaseService().saveOrUpdate(newsInfoContentHtml, NgNewsInfoContentHtml.class);
			} else {
				this.getBaseService().save(newsInfoContentHtml);
			}
		}

		return newListPage();
	}

	/**
	 * 添加图片组信息
	 * 
	 * @return
	 */
	public String pictureNewsInfoAdd() {
		newsInfo.setNewsCategory(null);
		newsInfo.setCreateAdmin("test");
		if (newsInfo.getState() == 1) {
			newsInfo.setPublishTime(new Timestamp(System.currentTimeMillis()));
		}
		newsInfo.setCreateTime(new Timestamp(System.currentTimeMillis()));
		newsInfo.setHeadImg(headImage);
		newsInfo.setInfotype(1);
		newsInfo.setDescription(content);
		if (null != newsInfo.getId()) {
			this.getBaseService().saveOrUpdate(newsInfo, NgNewsInfo.class);
		} else {
			this.getBaseService().save(newsInfo);
		}
 
		//如果数据库里有数据则删除
		List<NgNewsInfoContentPicture> list = this.getBaseService().findByProperty("newsInfoId", newsInfo.getId(), NgNewsInfoContentPicture.class);
		for (NgNewsInfoContentPicture picture : list) {
			this.getBaseService().delete(picture);
		}

		if (hindGroupImagInfo.contains(",")) {
			String[] images = hindGroupImagInfo.split(",");
			for (String imgString : images) {
				if (StringUtils.isEmpty(imgString)) {
					continue;
				}
				String[] temp = imgString.split(";");
				NgNewsInfoContentPicture newsInfoContentPicture = new NgNewsInfoContentPicture();
				newsInfoContentPicture.setCreateTime(new Timestamp(System.currentTimeMillis()));
				newsInfoContentPicture.setPictureSize(0);
				newsInfoContentPicture.setNewsInfoId(newsInfo.getId());

				if (temp.length > 0) {
					newsInfoContentPicture.setPictureAddress(temp[0]);
				}
				if (temp.length > 1) {
					newsInfoContentPicture.setPictureTitle(temp[1]);
				} else {
					newsInfoContentPicture.setPictureTitle("");
				}
				this.getBaseService().save(newsInfoContentPicture);
			}
		}

		return newInfoQuery2Page();
	}

	/**
	 * 修改HTML网页信息
	 * 
	 * @return
	 */
	public String editHTMLNewsInfo() {
		newsInfo = this.getBaseService().findById(optionId, NgNewsInfo.class);
		headImage = newsInfo.getHeadImg();
		optionStringId = newsInfo.getNewsCategory().getId() + "";
		List<NgNewsInfoContentHtml> list = this.getBaseService().findByProperty("newsInfoId", newsInfo.getId(), NgNewsInfoContentHtml.class);
		if (list.size() > 0) {
			newsInfoContentHtml = list.get(0);
			htmlContent = newsInfoContentHtml.getHtmlContent();
		}
		this.setTitle("修改HTML网页信息");
		return "newsAdd";
	}

	/**
	 * 修改图片位信息
	 * 
	 * @return
	 */
	public String editPicturePositionNewsInfo() {
		newsInfo = this.getBaseService().findById(optionId, NgNewsInfo.class);
		headImage = newsInfo.getHeadImg();
		List<NgNewsInfoContentHtml> list = this.getBaseService().findByProperty("newsInfoId", newsInfo.getId(), NgNewsInfoContentHtml.class);
		if (list.size() > 0) {
			newsInfoContentHtml = list.get(0);
			htmlContent = newsInfoContentHtml.getHtmlContent();
		}
		this.setTitle("修改图片位信息");
		return "picturePositionAdd";
	}

	
	
	/**
	 * 修改图片组信息
	 * 
	 * @return
	 */
	public String editPictireInfo() {
		newsInfo = this.getBaseService().findById(optionId, NgNewsInfo.class);
		headImage = newsInfo.getHeadImg();
		infoContentPictures=this.getBaseService().findByProperty("newsInfoId", newsInfo.getId(), NgNewsInfoContentPicture.class);
		
		this.setTitle("修改图片组信息");
		return "newsAddPicture";
	}

	/**
	 * 修改视频信息
	 * 
	 * @return
	 */
	public String editVideoInfo() {
		newsInfo = this.getBaseService().findById(optionId, NgNewsInfo.class);
		headImage = newsInfo.getHeadImg();
		List<NgNewsInfoContentVideo> list = this.getBaseService().findByProperty("newsInfoId", newsInfo.getId(), NgNewsInfoContentVideo.class);
		if (list.size() > 0) {
			newsInfoContentVideo = list.get(0);
			content = newsInfoContentVideo.getDescription();
		}
		return "newsAddVideo";
	}

	/**
	 * 查看信息
	 * 
	 * @return
	 */
	public String newsHtmlView() {
		newsInfo = this.getBaseService().findById(optionId, NgNewsInfo.class);
		List<NgNewsInfoContentHtml> list = this.getBaseService().findByProperty("newsInfoId", newsInfo.getId(), NgNewsInfoContentHtml.class);
		if (list.size() > 0) {
			newsInfoContentHtml = list.get(0);
		}
		return "newsHtmlView";
	}

	/**
	 * 不发布
	 * 
	 * @return
	 */
	public String stop() {
		newsInfo = this.getBaseService().findById(optionId, NgNewsInfo.class);
		newsInfo.setState(0);
		this.getBaseService().saveOrUpdate(newsInfo, NgNewsInfo.class);
		if(optionType==0){
			return newInfoQuery1Page();
		}else if(optionType==1){
			return newInfoQuery0Page();
		}else if(optionType==2){
			return newInfoQuery2Page();
		}else if(optionType==3){
			return newInfoQuery3Page();
		}else if(optionType==4){
			return newsPulishPage();
		}else if(optionType==5){
			return newsPulishedPage();
		}else if(optionType==6){
			return newsRemoveListPage();
		}
		return newListPage();
	}

	/**
	 * 发布
	 * 
	 * @return
	 */
	public String start() {
		newsInfo = this.getBaseService().findById(optionId, NgNewsInfo.class);
		if (newsInfo.getState() == 0) {
			newsInfo.setState(1);
			newsInfo.setPublishTime(new Timestamp(System.currentTimeMillis()));
			this.getBaseService().saveOrUpdate(newsInfo, NgNewsInfo.class);
		}
		if(optionType==0){
			return newInfoQuery1Page();
		}else if(optionType==1){
			return newInfoQuery0Page();
		}else if(optionType==2){
			return newInfoQuery2Page();
		}else if(optionType==3){
			return newInfoQuery3Page();
		}else if(optionType==4){
			return newsPulishPage();
		}else if(optionType==5){
			return newsPulishedPage();
		}else if(optionType==6){
			return newsRemoveListPage();
		}
		return newListPage();
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	public String remove() {
		newsInfo = this.getBaseService().findById(optionId, NgNewsInfo.class);
		newsInfo.setState(2);
		this.getBaseService().saveOrUpdate(newsInfo, NgNewsInfo.class);
		if(optionType==0){
			return newInfoQuery1Page();
		}else if(optionType==1){
			return newInfoQuery0Page();
		}else if(optionType==2){
			return newInfoQuery2Page();
		}else if(optionType==3){
			return newInfoQuery3Page();
		}else if(optionType==4){
			return newsPulishPage();
		}else if(optionType==5){
			return newsPulishedPage();
		}else if(optionType==6){
			return newsRemoveListPage();
		}
		return newsRemoveListPage();
	}

	/**
	 * 恢复
	 * 
	 * @return
	 */
	public String reRemove() {
		newsInfo = this.getBaseService().findById(optionId, NgNewsInfo.class);
		if (newsInfo.getState() == 2) {
			newsInfo.setState(0);
			this.getBaseService().saveOrUpdate(newsInfo, NgNewsInfo.class);
		}
		if(optionType==0){
			return newInfoQuery1Page();
		}else if(optionType==1){
			return newInfoQuery0Page();
		}else if(optionType==2){
			return newInfoQuery2Page();
		}else if(optionType==3){
			return newInfoQuery3Page();
		}else if(optionType==4){
			return newsPulishPage();
		}else if(optionType==5){
			return newsPulishedPage();
		}else if(optionType==6){
			return newsRemoveListPage();
		}
		return newsPulishPage();
	}

	/**
	 * @return the newsInfo
	 */
	public NgNewsInfo getNewsInfo() {
		return newsInfo;
	}

	/**
	 * @param newsInfo
	 *            the newsInfo to set
	 */
	public void setNewsInfo(NgNewsInfo newsInfo) {
		this.newsInfo = newsInfo;
	}

	/**
	 * @return the categoryList
	 */
	public List<NgNewsCategory> getCategoryList() {
		// categoryList = this.getBaseService().findByProperty("state", 0,
		// NgNewsCategory.class);
		categoryList = this.getBaseService().findByProperty("state", 0, NgNewsCategory.class, "oder", "ASC");
		return categoryList;
	}

	/**
	 * @param categoryList
	 *            the categoryList to set
	 */
	public void setCategoryList(List<NgNewsCategory> categoryList) {
		this.categoryList = categoryList;
	}

	/**
	 * @return the optionStringId
	 */
	public String getOptionStringId() {
		return optionStringId;
	}

	/**
	 * @param optionStringId
	 *            the optionStringId to set
	 */
	public void setOptionStringId(String optionStringId) {
		this.optionStringId = optionStringId;
	}

	/**
	 * @return the htmlContent
	 */
	public String getHtmlContent() {
		// htmlContent = htmlContent.replaceAll("&", "&amp;");
		// htmlContent = htmlContent.replaceAll("<", "&lt;");
		// htmlContent = htmlContent.replaceAll(">", "&gt;");
		// htmlContent = htmlContent.replaceAll("\"", "&quot;");
		return htmlContent;
	}

	/**
	 * @param htmlContent
	 *            the htmlContent to set
	 */
	public void setHtmlContent(String htmlContent) {
		this.htmlContent = htmlContent;
	}

	/**
	 * @return the newsInfoContentHtml
	 */
	public NgNewsInfoContentHtml getNewsInfoContentHtml() {
		return newsInfoContentHtml;
	}

	/**
	 * @param newsInfoContentHtml
	 *            the newsInfoContentHtml to set
	 */
	public void setNewsInfoContentHtml(NgNewsInfoContentHtml newsInfoContentHtml) {
		this.newsInfoContentHtml = newsInfoContentHtml;
	}

	/**
	 * @return the optionId
	 */
	public Long getOptionId() {
		return optionId;
	}

	/**
	 * @param optionId
	 *            the optionId to set
	 */
	public void setOptionId(Long optionId) {
		this.optionId = optionId;
	}

	/**
	 * @return the headImage
	 */
	public String getHeadImage() {
		return headImage;
	}

	/**
	 * @param headImage
	 *            the headImage to set
	 */
	public void setHeadImage(String headImage) {
		this.headImage = headImage;
	}

	/**
	 * @return the hindGroupImagInfo
	 */
	public String getHindGroupImagInfo() {
		return hindGroupImagInfo;
	}

	/**
	 * @param hindGroupImagInfo
	 *            the hindGroupImagInfo to set
	 */
	public void setHindGroupImagInfo(String hindGroupImagInfo) {
		this.hindGroupImagInfo = hindGroupImagInfo;
	}

	/**
	 * @return the newsInfoContentVideo
	 */
	public NgNewsInfoContentVideo getNewsInfoContentVideo() {
		return newsInfoContentVideo;
	}

	/**
	 * @param newsInfoContentVideo
	 *            the newsInfoContentVideo to set
	 */
	public void setNewsInfoContentVideo(NgNewsInfoContentVideo newsInfoContentVideo) {
		this.newsInfoContentVideo = newsInfoContentVideo;
	}

	/**
	 * @return the content
	 */
	public String getContent() {
		return content;
	}

	/**
	 * @param content
	 *            the content to set
	 */
	public void setContent(String content) {
		this.content = content;
	}

	/**
	 * @return the infoContentPictures
	 */
	public List<NgNewsInfoContentPicture> getInfoContentPictures() {
		return infoContentPictures;
	}

	/**
	 * @param infoContentPictures the infoContentPictures to set
	 */
	public void setInfoContentPictures(List<NgNewsInfoContentPicture> infoContentPictures) {
		this.infoContentPictures = infoContentPictures;
	}

	/**
	 * @return the optionType
	 */
	public int getOptionType() {
		return optionType;
	}

	/**
	 * @param optionType the optionType to set
	 */
	public void setOptionType(int optionType) {
		this.optionType = optionType;
	}

}
